package com.tuangou.tuangouorder.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.tuangou.tuangouorder.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCq2eYUwE5ogs1NC6UFbqDu3/zPyxwyZ/Pjj/itIe/Ds4zqz91GTOVGBx6IYsrNM4lPdKk5wpEj4Hf/WrrMh0pNZtOgbMEccuWAeRUG4djxAh5s1vhKqqEKTHA9GDglMP7vqqwRtp/NzY6PUnuRqBwhyrn3d2OqJ+avSyja+7mRvIBCk5FoFvkyAwBI25sa4OsBa5BdSvHQaVvE/OFf2wzTFlrYRvJ+t90s/f38zjlP4wu2wST9LcLBwfJucEnUO7dBiGNePnDPNbQ5Ie30BuyLLfNRgCQopR02nKqBjqJD3PBYBqveJ22HcgH0fbNMpK7yP/BE0239ii5CGkE15w/JAgMBAAECggEAPqe+SunjV0xwwcp9cCqL9k5KRmH0K0AYHuITjlqdtSVJjmtAeKGGi1HdDqM8QFigCHV+AeKqm3+cQhch3hPXWd9VyMVZZHd2VtEQtv9oMapXQg7r62X7vO0fVChnnqwDMh1jEPPqUu0JVOIo0tfVyUaxQGGWOTZ80Ng9WWFrQlhHFx4RDJou9PwlnlYaQzDmtyDSzJ4CkU62UxecVEZKrEIsmrPU1+bbHmzboTE7VfpBDCVMReSxO7HY60WvTlh9p6pCQqdPUuDrAZKcG7RiYtKjHkxSyW5CuvMhYNXkegCWS0Ape9qx5fBxB2Ad5eQ4ziPrNLJtHxSLjO2MfFGwIQKBgQDbcZFtYFgi/1MPsg9Hv9OpE16oIAJAp6Trp6Zj692nDOymSpZzFt6yudNHSyecrAmzevzoPdLtIY2iLqNk1hnSOPL6xNDXxUxBKujoa83f0odSY/LmQ6Zd8LFvIA/42axcklET84Wak7cGVlKqWlONLIEg5ceXC2Vcu9pAQ+RPnQKBgQDHUAxM8k0lf0gaD6dmnwn/yITom4U+iMRbTbJa/F2y8xUNp70SRb7CpbK709VNgdqBYwu/XaCBTcVcMDipNzodyJGNsx/R+uP/PxMwy6Rfv4VajbxwIUWZiEeZU/8QLq3AXI1YDKwziKg5zb9FILw3KC30n7n+rned+fUAZJPHHQKBgQDMZIFh8OTm5A4Hhn/5AlExIBuAWR///KX94M94Lk7rs3wQf1SWqeyPZ8nEk9m3cf4vMBS0dDEsMZ5sOyhxWYH/bo/SxtMGxnv6Du+U3CSLntnwAaxSkRw6T1/U83YtAQIrzxS5ko8UpRVKT8j7Ztp/mb5EWAGJTFIDB+2HAjV7eQKBgQCtFD9KRpNlv40OTI+zFuKpAykfkYX40u2o1+TNDCAV+O9MZ2BR0M0h3KBEaWTupJfqCee2q4hpIO8E6j8Z+wJ1wKldIrK8NqHtEooIsp6wIvGbvD9qJAp7lBOQ+5cgJ1jtFbIMRxz2t3JWl+vEDPRE9ZhKuLXciwcG4Xu3dp+5pQKBgQDHrdoTida+48azAv0/Kqyfm7aou2VDfv8PjC+OdKqUl8D3xF1E0qQBolWj5BVRnwh2QAwgtpE7l75zPlo+brCiywMcw64UV0NuWg7worIje3GHds6KAAMSXyJJrNWW/mGa+CMqYSUvBmyRaT/LG399XbRO8Kt1HMZwiCeign7hVg==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key;
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
